﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.interfaces;
using umbraco.BusinessLogic;
using umbraco.BasePages;
using System.Xml;

namespace uREST.Core.PackageActions
{
    public abstract class AbstractPackageAction : IPackageAction
    {
        public abstract string Alias();
        public abstract bool Execute(string packageName, System.Xml.XmlNode xmlData);
        public abstract bool Undo(string packageName, System.Xml.XmlNode xmlData);

        /// <summary>
        /// Returns a Sample XML Node  
        /// </summary>
        /// <returns>The sample xml as node</returns>
        public virtual System.Xml.XmlNode SampleXml()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Get the current user, or when unavailable admin user
        /// </summary>
        /// <returns>The current user</returns>
        protected User GetUser()
        {
            int id = BasePage.GetUserId(BasePage.umbracoUserContextID);
            id = (id < 0) ? 0 : id;
            return User.GetUser(id);
        }

        /// <summary>
        /// Get a named attribute from xmlData node
        /// </summary>
        /// <param name="xmlData">The xml data</param>
        /// <param name="attribute">The name of the attribute</param>
        /// <param name="value">returns the attribute value from xmlData</param>
        /// <returns>True, when attribute value available</returns>
        protected bool TryGetAttribute(XmlNode xmlData, string attribute, out string value)
        {
            //Set result default to false
            bool result = false;

            //Out params must be assigned
            value = String.Empty;

            //Search xml attribute
            XmlAttribute xmlAttribute = xmlData.Attributes[attribute];

            //When xml attribute exists
            if (xmlAttribute != null)
            {
                //Get xml attribute value
                value = xmlAttribute.Value;

                //Set result successful to true
                result = true;
            }
            else
            {
                //Log error message
                Log.Add(LogTypes.Error, GetUser(), -1, "Package Action Error: Attribute \"" + attribute + "\" not found.");
            }
            return result;
        }

        /// <summary>
        /// Get an optional named attribute from xmlData node
        /// when attribute is unavailable, return the default value
        /// </summary>
        /// <param name="xmlData">The data that must be appended to the web.config file</param>
        /// <param name="attribute">The name of the attribute</param>
        /// <param name="defaultValue">The default value</param>
        /// <returns>The attribute value or the default value</returns>
        protected string GetAttributeDefault(XmlNode xmlData, string attribute, string defaultValue)
        {
            //Set result default value
            string result = defaultValue;

            //Search xml attribute
            XmlAttribute xmlAttribute = xmlData.Attributes[attribute];

            //When xml attribute exists
            if (xmlAttribute != null)
            {
                //Get available xml attribute value
                result = xmlAttribute.Value;
            }

            return result;
        }
    }
}
